package tables

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

type Category struct {
	Name     string `gorm:"column:category_name;type:varchar(60);not null;index:idx_category_name"`
	ImageUrl string `gorm:"column:category_image_url;type:varchar(300);not null"`
	Goods    []Goods
	IsHot    int32  `gorm:"column:category_is_hot;type:tinyint;default:0;"`
	BgColor  string `gorm:"column:category_bg_color;type:varchar(20);default:white;"`
	HotImage JSON   `gorm:"column:category_hot_image;type:json;"`
	BaseModel
}

func (r *Category) TableName() string {
	return "category"
}

func (r *Category) CategoryList(ctx *gin.Context, db *gorm.DB) (*[]Category, *int64, error) {
	category := new([]Category)
	total := new(int64)
	result := db.WithContext(ctx).Model(&Category{}).Find(category).Count(total)
	if result.Error != nil {
		empty := new(int64)
		return nil, empty, result.Error
	}

	return category, total, nil
}

func (r *Category) CategoryHot(ctx *gin.Context, db *gorm.DB) (*[]Category, error) {
	category := new([]Category)
	result := db.WithContext(ctx).Model(&Category{}).Where("category_is_hot = ?", 1).Find(category)
	if result.Error != nil {
		return nil, result.Error
	}

	return category, nil
}
